#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "hplog.h"
#include "../log_client_api.h"

#ifndef false
#define false 0
#endif

#ifndef true
#define true  1
#endif

log_client_conf conf;

/*
 * Class:     hplog
 * Method:    init_hplog
 * Signature: ()I
 */
JNIEXPORT jint JNICALL Java_hplog_init_1java_1hplog
  (JNIEnv *env, jobject obj)
{
    int iret = NOERR;
    iret = init_hplog(&conf);
    return iret;
}

/*
 * Class:     hplog
 * Method:    dyn_set_log_level
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_hplog_dyn_1set_1log_1level
  (JNIEnv *env, jobject obj, jint log_level)
{
    return set_client_log_level(log_level);
}

/*
 * Class:     hplog
 * Method:    tx_log
 * Signature: ()I
 */
JNIEXPORT jint JNICALL Java_hplog_tx_1log
  (JNIEnv *env, jobject obj, jint level, jstring msg)
{
    const char* str;
    str = (*env)->GetStringUTFChars(env,msg,false);
    if(str == NULL)
    {
        (*env)->ReleaseStringUTFChars(env,msg,str);
        return INVIO;
    }
    else
    {
        /*send the log msg to the log daemon*/
        hplog_output(level,str);
    }
    
    (*env)->ReleaseStringUTFChars(env,msg,str);

	return NOERR;
}

/*
 * Class:     hplog
 * Method:    set_local_log_path
 * Signature: (Ljava/lang/String;)I
 */
JNIEXPORT jint JNICALL Java_hplog_set_1local_1log_1path
  (JNIEnv *env, jobject obj, jstring path)
{
    const char* str;
    str = (*env)->GetStringUTFChars(env,path,false);
    if(str == NULL)
    {
        (*env)->ReleaseStringUTFChars(env,path,str);
        return INVIO;
    }
    else
    {
        strncpy(conf.local_log_path,str,MAX_LOG_PATH_SIZE);
    }
    
    (*env)->ReleaseStringUTFChars(env,path,str);

	return NOERR;
}

/*
 * Class:     hplog
 * Method:    set_local_log_size
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_hplog_set_1local_1log_1size
  (JNIEnv *env, jobject obj, jint size)
{
    conf.max_log_size =size;
	return NOERR;
}

/*
 * Class:     hplog
 * Method:    set_log_init_level
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_hplog_set_1log_1init_1level
  (JNIEnv *env, jobject obj, jint log_level)
{
    if((log_level <0) || (log_level >8))
        conf.log_severity = 0;
    else
        conf.log_severity = log_level;
	return NOERR;
}

/*
 * Class:     hplog
 * Method:    set_log_process_name
 * Signature: (Ljava/lang/String;)I
 */
JNIEXPORT jint JNICALL Java_hplog_set_1log_1process_1name
  (JNIEnv *env, jobject obj, jstring name)
{    
    const char* str;
    str = (*env)->GetStringUTFChars(env,name,false);
    if(str == NULL)
    {
        (*env)->ReleaseStringUTFChars(env,name,str);
        return INVIO;
    }
    else
    {
        strncpy(conf.process_name,str,MAX_PROCESS_NAME_SIZE);
    }
    
    (*env)->ReleaseStringUTFChars(env,name,str);

	return NOERR;
}

/*
 * Class:     hplog
 * Method:    set_log_flags
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_hplog_set_1log_1flags
  (JNIEnv *env, jobject obj, jint flags)
{
    conf.flags = flags;
	return NOERR;
}

